<template>
	<view class="content">
		<view class="avatorWrapper">
			<view class="avator">
				<image class="img" src="/static/logo.png" mode="widthFix"></image>
			</view>
		</view>
		<view class="form">
			<uni-forms  ref="form" :modelValue="formData">
				<uni-forms-item name="userName">
					<input class="input" type="text" value="" v-model="formData.userName" placeholder="请输入用户名" />
				</uni-forms-item>
				<uni-forms-item name="password">
					<input class="input" type="password" value="" v-model="formData.password" placeholder="请输入密码" />
				</uni-forms-item>
			</uni-forms>

			<view class="loginBtn" @click="submit">
				<text class="btnValue">登录</text>
			</view>
		</view>


		<!-- <view class="form">
			<view class="inputWrapper">
				<input class="input" type="text" value="" v-model="userName"  placeholder="请输入手机号"/>
			</view>
			<view class="inputWrapper">
				<input class="input" type="password" value="" v-model="password" placeholder="请输入密码"/>
			</view>
			<view class="loginBtn">
				<text class="btnValue" @click="login">登录</text>
			</view> -->
			
			<!-- <button  @click="submit">登录</button> -->
		<view class="forgotBtn" @click="navToRegister">
			<text>- 注册 -</text>
		</view>
		
	</view>
	</view>
</template>



<script>
	export default {
		data() {
			return {
				formData: {
					userName: 'ggdog',
					password:'ggdog',
				},
				// rules: {
				// 	iphone: {
				// 		rules: [{
				// 			required: true,
				// 			errorMessage: '请输入注册手机号码'
				// 		}, {
				// 			validateFunction: function(rule, value, data, callback) {
				// 				let iphoneReg = /^1[0-9]{10}$/
				// 				if (!iphoneReg.test(value)) {
				// 					callback('手机号码格式不正确，请重新填写')
				// 				}
				// 				return true
				// 			}
				// 		}]
				// 	}
				// }
			}
		},
		onLoad() {

		},
		methods: {
			navToRegister(){
				uni.navigateTo({
					url:"/pages/login/register"
				})
			},
			 submit() {
			            this.$refs.form.validate().then(res=>{
			                console.log('表单数据信息：', res);
							this.login()
			            }).catch(err =>{
			                console.log('表单错误信息：', err);
			            })
			        },
			async login() {
				await uni.request({
					url:"http://hkg.ggdogweb.top:8085/blogapi/user/login",
					method:'POST',
					data:{
						userName:this.formData.userName,
						userPsd:this.formData.password
					},
					success: (res) => {
						console.log(res.data)
						if(res.data.code===0){
							uni.showToast({
								title:"登录成功！"
							})
							var token=res.data.data
							uni.setStorage({
								key:"token",
								data:token
							})
							// 触发事件通知 my 组件更新
							uni.$emit('userLoggedIn', token);
							   // 这里可以使用 uni.navigateBack 返回到包含 my 组件的页面
							 uni.navigateBack()
							
						}else{
							uni.showToast({
								title:res.data.message,
								icon:'error'
							})
						}
						
					},
					fail: (err) => {
						uni.showModal({
							title:"登录失败！"+err
						})
						console.log("登录失败！"+err)
					}
				})
			},




		}
	}
</script>



<style>
	.content {
		width: 100vw;
		height: 100vh;
	}

	.avatorWrapper {
		margin-top: 16px;
		height: 30vh;
		width: 100vw;
		display: flex;
		justify-content: center;
		align-items: flex-end;
	}

	.avator {
		width: 300upx;
		height: 300upx;
		overflow: hidden;
	}

	.avator .img {
		width: 100%
	}

	.form {
		padding: 0 100upx;
		margin-top: 40px;
	}

	.input {
		flex: 1;
		font-size: 14px;
		color: #666;
		border: 1px #e5e5e5 solid;
		border-radius: 5px;
		padding: 10px;
	}

	.loginBtn {
		width: 100%;
		height: 80upx;
		background: #6290f3;
		border-radius: 50upx;
		margin-top: 50px;
		display: flex;
		justify-content: center;
		align-items: center;

	}

	.loginBtn .btnValue {
		color: white;
	}

	.forgotBtn {
		text-align: center;
		color: #8d8d8d;
		font-size: 15px;
		margin-top:20px;
	}
</style>
